using System;
using System.Collections;
using UnityEngine;

public class GGApplication : BehaviourSingleton<GGApplication>
{
	public class OnQuitBehaviour
	{
		public Action action;

		public bool isFinished;

		public void Run()
		{
			if (action == null)
			{
				isFinished = true;
			}
			else
			{
				action();
			}
		}
	}

	private OnQuitBehaviour onQuitBehaviour;

	private Coroutine quittingCoroutine;

	public void AddOnQuitBehaviour(OnQuitBehaviour behaviour)
	{
		Debug.Log("GGApplication.instance.AddOnQuitBehaviour");
		onQuitBehaviour = behaviour;
	}

	public void Quit()
	{
		Debug.Log("GGApplication.instance.Quit");
		if (quittingCoroutine == null)
		{
			if (onQuitBehaviour != null)
			{
				onQuitBehaviour.Run();
			}
			quittingCoroutine = DoQuit();
		}
	}

	private Coroutine DoQuit()
	{
		return StartCoroutine(DoQuitExecute());
	}

	private IEnumerator DoQuitExecute()
	{
		Debug.Log("GGApplication.instance.DoQuitExecute");
		while (onQuitBehaviour != null && !onQuitBehaviour.isFinished)
		{
			yield return null;
		}
		Debug.Log("Quitting");
		Application.Quit();
	}

	public void CancelQuit()
	{
		Debug.Log("GGApplication.instance.CancelQuit");
		if (quittingCoroutine != null)
		{
			StopCoroutine(quittingCoroutine);
			quittingCoroutine = null;
		}
		Application.CancelQuit();
	}
}
